package com.trkj.share;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.BitmapUtils;
import com.shamanland.fonticon.FontIconDrawable;
import com.trkj.base.BaseDataRefreshFragment;
import com.trkj.base.Constants;
import com.trkj.taoruiyun.R;
import com.trkj.widget.refresh.SwipeRefreshLayout;

import java.text.MessageFormat;

/**
 * 技术文章
 * Created by LiZanhong on 2015/06/20.
 */
public class TechnologyFragment extends BaseDataRefreshFragment{
    private int typeId; //文章类型的id，分别为1、2、3、4

    private BitmapUtils bitmapUtils;


    public int getTypeId() {
        return Constants.Share.SHARE_TYEP_TECHNOLOGY;
    }

    /**
     *
     *
     * @return
     */
    public String getUrl() {
        String url = MessageFormat.format(
                Constants.ApiUrl.SHARE_TECHNOLOGY_ARTICLE,
                getTypeId(),
                getPage(),
                getRows());
        Log.i("URL", url);
        return url;
    }


    @Override
    protected SwipeRefreshLayout.Mode getMode() {
        return SwipeRefreshLayout.Mode.BOTH;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //先定义Adapter
        if(getAdapter() == null) {
            setAdapter(new ArticleAdapter());
        }
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bitmapUtils = new BitmapUtils(this.getActivity());
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        long articleId = parent.getAdapter().getItemId(position);
        Intent intent = new Intent(ArticleDetailActivity.ACTION);
        intent.putExtra("articleId", articleId);
        startActivity(intent);
    }

    class ArticleAdapter extends BaseAdapter {
        @Override
        public int getItemViewType(int position) {
            JSONObject object = getData().getJSONObject(position);
            if(TextUtils.isEmpty(object.getString("cover"))){
                return 0; //不带图片
            }else{
                return 1; //带图片
            }
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }

        @Override
        public int getCount() {
            return getData().size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return getData().getJSONObject(position).getIntValue("article_id");
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder viewHolder;
            if (convertView == null) {

                if(getItemViewType(position) == 1){
                    //////////////带图片的/////////
                    convertView = LayoutInflater.from(getActivity()).inflate(
                            R.layout.fragment_share_item_image, null);
                }else {
                    //////////////不带图片的/////////
                    convertView = LayoutInflater.from(getActivity()).inflate(
                            R.layout.fragment_share_item, null);
                }

                viewHolder = new ViewHolder();
                viewHolder.articleTitle = (TextView) convertView.findViewById(R.id.article_title);
                viewHolder.shareDate = (TextView) convertView.findViewById(R.id.share_date);
                viewHolder.shareAuthor = (TextView) convertView.findViewById(R.id.share_author);
                viewHolder.shareReason = (TextView) convertView.findViewById(R.id.share_reason);

                viewHolder.rightArrow = (ImageView) convertView.findViewById(R.id.right_arrow);

                //////////////带图片的/////////
                if(getItemViewType(position) == 1){
                    viewHolder.conver = (ImageView) convertView.findViewById(R.id.conver);
                }

                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            JSONArray data = getData();

            viewHolder.articleTitle.setText(Html.fromHtml(
                    data.getJSONObject(position).getString("title_")));
            viewHolder.shareDate.setText(
                    data.getJSONObject(position).getString("date_").substring(0, 11));

            String author = data.getJSONObject(position).getString("author");
            if (TextUtils.isEmpty(author)) {
                viewHolder.shareAuthor.setVisibility(View.GONE);
            } else {
                viewHolder.shareAuthor.setVisibility(View.VISIBLE);
                viewHolder.shareAuthor.setText(author);
            }
            viewHolder.rightArrow.setImageDrawable(
                    FontIconDrawable.inflate(getActivity(), R.xml.right_arrow));

            String reason = data.getJSONObject(position).getString("reason_");
            if (TextUtils.isEmpty(reason)) {
                viewHolder.shareReason.setVisibility(View.GONE);
            } else {
                viewHolder.shareReason.setVisibility(View.VISIBLE);
                viewHolder.shareReason.setText(reason);
            }

            //////////////带图片的/////////
            if(getItemViewType(position) == 1){
                String image = data.getJSONObject(position).getString("cover");
                if(!image.startsWith("http")){
                    image = "http://gson.cn" + image;
                }
                 bitmapUtils.display(viewHolder.conver, image);
            }

            return convertView;
        }

        class ViewHolder {
            TextView articleTitle;
            ImageView rightArrow;
            TextView shareDate;
            TextView shareAuthor;
            TextView shareReason;
            ImageView conver;
        }
    }
}
